package middleware

import (
	"base-web/api/reponse"
	"base-web/util/jwt_auth"
	"github.com/gin-gonic/gin"
	"time"
)

//jwt权限验证
func JwtAuth() gin.HandlerFunc {
	return func(c *gin.Context) {

		user, err := jwt_auth.ParseToken(c)
		if err != nil {
			reponse.Result(reponse.ERROR, nil, err.Error(), c)
			c.Abort()
			return
		}
		if user.ExpiresAt <= time.Now().Unix() {
			reponse.Result(reponse.ERROR, nil, "身份已过期", c)
			c.Abort()
			return
		}

		c.Set("user_id", user.ID)
		c.Next()
	}
}
